import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiandou/Common.dart';
import 'package:xiandou/Request/DioManager.dart';
import 'package:xiandou/Request/NWApi.dart';
import 'package:xiandou/Views/XDWebViewController.dart';
import 'package:xiandou/personalcenter/AboutUsController.dart';
import 'package:xiandou/personalcenter/HelpCenterController.dart';
import 'package:xiandou/personalcenter/OrderListController.dart';
import 'package:xiandou/personalcenter/GoldListController.dart';
import 'package:xiandou/Request/NWMethod.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:xiandou/personalcenter/SettingController.dart';
import 'package:xiandou/personalcenter/UserProfileController.dart';
import 'package:xiandou/personalcenter/WithdrawalController.dart';
import 'package:xiandou/personalcenter/inviteController.dart';

class PersonalView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => PersonalViewScreenState();
}

class PersonalViewScreenState extends State<PersonalView> with AutomaticKeepAliveClientMixin {

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    getCurrentUserInfo();
    super.initState();
  }

  String balance = '0';
  String nickName = '开开心心一只猪';

  void _pushToGoldCoinsController() {
    Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => new ListViewScreen()),
    );
  }

  void _pushToUserProfileController() {
    Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => new UserProfileScreen()),
    );
  }

  void _pushToWithdrawalController() {
    Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => new WithdrawalControllerScreen()),
    );
  }

  void _pushToSettingController() {
    Navigator.push(
      context,
      new MaterialPageRoute(builder: (context) => new SettingrScreen()),
    );
  }

  void getCurrentUserInfo (){
    DioManager().request(NWMethod.GET, NWApi.getUserInfoApi,params: null,success: (data){
      print(data);
      balance = data['balance'].toString();
      nickName = data['nickname'].toString();
      setState(() {

      });
    },error: (error){
      Fluttertoast.showToast(msg: error.message);
    });
  }

  // 任务详情widget
  Widget taskWidget(String imageName,String title,int index){
    return new GestureDetector(
      onTap: (){
        Navigator.push(
          context,
          new MaterialPageRoute(builder: (context) => new OrderListViewScreen(categoryId: index,)),
        );
      },
      child:Container(
        child: Column(
          children: <Widget>[
            Image(image: AssetImage(imageName),width: 25,height: 25,),
            Text(title)
          ],
        ),
      ),
    );
  }

  Widget myListCellWidget(String imageName,String title,int index){
    return new GestureDetector(
      child:ListTile(
        onTap: (){
          if(index == 0){
            Navigator.push(
              context,
              new MaterialPageRoute(builder: (context) => new InviteViewScreen()),
            );
          }else if(index == 1){
            Navigator.push(
              context,
              new MaterialPageRoute(builder: (context) => new XDWebViewController(title: '百度',url: 'https://www.baidu.com',)),
            );
          }else {
            Navigator.push(
              context,
              new MaterialPageRoute(builder: (context) => new AboutUsScreen()),
            );
          }
        },
        leading: CircleAvatar(
          backgroundImage: AssetImage(imageName),
          backgroundColor: Colors.white,
        ),
        title: Text(title),
        contentPadding: const EdgeInsets.fromLTRB(16, 5, 16, 5),
        trailing: Icon(Icons.keyboard_arrow_right),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.white,
        middle: const Text('个人中心'),
        transitionBetweenRoutes:false,
//        heroTag: 'personalProfile',
        trailing: GestureDetector(
          onTap: _pushToSettingController,
          child: Icon(Icons.settings,size: 17,),
        )
      ),
      body:Container(
        color: Color(0xffF6F6F6),
        child: ListView(
          children: <Widget>[
            Stack(
              alignment:Alignment.center,
              overflow: Overflow.visible,
//              fit: StackFit.expand,
              children: <Widget>[
                Container(
                  height: 270,
                ),
                Positioned(
                  top: 0,
                  right: 0,
                  left: 0,
                  child: Container(
                    color: Colors.white,
                    height: 132,
                    padding: const EdgeInsets.fromLTRB(11, 31, 21, 10),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Expanded(
                          child:Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text('$nickName',style: TextStyle(fontSize: 17,fontWeight: FontWeight.bold,color: Common.blackColor),),
                              SizedBox(height: 5,),
                              Text('一天很短，开心最重要',style: TextStyle(fontSize: 14,color:Common.darkColor),)
                            ],
                          ),
                        ),
                        GestureDetector(
                          onTap: _pushToUserProfileController,
                          child: Container(
                            width: 45,
                            height: 45,
                            decoration: new BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(22.5)),
                                border: Border.all(color: Common.xdOrangeColor, width: 1.5),
                                image: DecorationImage(
                                  image:new NetworkImage('http://h.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=0d023672312ac65c67506e77cec29e27/9f2f070828381f30dea167bbad014c086e06f06c.jpg'),
                                )
                            ),
                          )
                        )
                      ],
                    ),
                  ),
                ),
                Positioned(
                  top: 110,
                  left: 10,
                  right: 10,
                  bottom: 5,
                  child: Container(
                    height: 142,
                    padding: const EdgeInsets.only(top: 25),
                    decoration: new BoxDecoration(
                      color: Color(0xffFF5236),
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: <Widget>[
                        Text('账号余额（闲豆）',style: TextStyle(color: Colors.white,fontSize: 14),),
                        Padding(
                          padding: const EdgeInsets.only(top: 8),
                          child: Text('$balance',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 26,color: Colors.white),),
                        ),
                        Container(
                          height: 1,
                          width: 65,
                          color: Colors.white,
                        ),
                        Expanded(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: <Widget>[
                              FlatButton(
                                child: Text('金币记录',style: TextStyle(color: Colors.white),),
                                onPressed: _pushToGoldCoinsController,
                              ),
                              FlatButton(
                                child: Text('申请提现',style: TextStyle(color: Colors.white),),
                                onPressed: _pushToWithdrawalController,
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                )
              ],
            ),
            Container(
              margin: const EdgeInsets.fromLTRB(10, 10, 10, 0),
//              padding: const EdgeInsets.fromLTRB(22, 22, 0, 0),
              height: 127,
              decoration: new BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(5))
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    padding: const EdgeInsets.fromLTRB(25, 23, 0, 12) ,
                    child: Text('任务列表',style: TextStyle(color: Color(0xff333333)),),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      taskWidget('assets/d_all.png','全部',0),
                      taskWidget('assets/d_confirm.png','进行中',1),
                      taskWidget('assets/d_unuse.png','审核中',2),
                      taskWidget('assets/d_comment.png','已完成',3),
                      taskWidget('assets/d_failure.png','审核失败',4),
                    ],
                  )
                ],
              ),
            ),
            Container(
              height: 200,
              margin: const EdgeInsets.fromLTRB(10, 15, 10, 20),
              decoration: new BoxDecoration(
                color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(5))
              ),
              child: ListView(
                physics: new NeverScrollableScrollPhysics(),
                children: ListTile.divideTiles(
                    context: context,
                    tiles: [
                      myListCellWidget('assets/d_contact.png','我的邀请',0),
                      myListCellWidget('assets/d_help.png','帮助中心',1),
                      myListCellWidget('assets/d_invite.png','联系我们',2),
                    ],
                  ).toList(),
              ),
            )
          ],
        ),
      )
    );
  }
}
